<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.filesystem.file');
class upravaFaktury extends JTable
{
	/**
	 * Primary Key
	 * @var int
	 */
	var $id 				= null;
	/** @var string */
	var $title 				= '';
	/** @var string */
	var $fulltext 				= '';
	/** @var string */
	var $jedcislo 				= '';
/** @var date */
	var $publish_up 	= 0;
/** @var date */
	var $publish_down 	= 0;
	/** @var int */
	var $catid 	= 1;
	/** @var string */
	var $created_by_alias 	= '';
	/** @var string */
	var $vlastni1 	= '';
	/** @var string */
	var $vlastni2 	= '';
	/** @var string */
	var $file 				= '';
	// zverejnovanie
  /** @var string */
	var $faktura 				= '';
  /** @var string */
	var $intCislo 				= '';
  /** @var string */
	var $dodavatel 				= 0;
  /** @var string */
	var $suma 				= '';
  /** @var string */
	var $datUhrady 				= 0;
  /** @var string */
	var $datZver 				= 0;
  /** @var string */
	var $sposob 				= '';
  /** @var string */
	var $poznamka 				= '';
  /** @var string */
	var $file2 				= '';
  /** @var string */
	var $mena 				= '';
  /** @var string */
	var $typ 				= '';
  /** @var int */
	var $sposob_uhrady 	= '';
    /** @var int */
	var $hits 	= 0;
    /** @var int */
	var $hits2 	= 0;
      /** @var int */
	var $hits_detail 	= 0;
  
  
  /** @var string */
//	var $alias	 			= '';
	/** @var int */
//	var $checked_out 		= 0;
	/** @var date */
//	var $checked_out_time 	= 0;

	function upravaFaktury(& $db) {
    
    $this->params	= &JComponentHelper::getParams('com_jzmluvy');
  
		parent::__construct('#__jzmluvy', 'id', $db);
	}

	// overloaded check function
	function check()
	{
/*		// Check fields
		if (empty($this->enddates)) {
			$this->enddates = NULL;
		}

		if (empty($this->times)) {
			$this->times = NULL;
		}

		if (empty($this->endtimes)) {
			$this->endtimes = NULL;
		}

		$this->title = strip_tags(trim($this->title));
		$titlelength = JString::strlen($this->title);

		if ( $this->title == '' ) {
			$this->_error = JText::_( 'ADD TITLE' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}

		if ( $titlelength > 100 ) {
      		$this->_error = JText::_( 'ERROR TITLE LONG' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}*/
    
    //*** vytahuje info z config.xml
    $platnost = JRequest::getVar( 'platnost', '0', '', 'int' );

    $this->title = htmlspecialchars($this->title);
		$alias = JFilterOutput::stringURLSafe($this->title);

		if(empty($this->alias) || $this->alias === $alias ) {
			$this->alias = $alias;
		}
    
    
    //*** povinne pole pre dodavatela
    if ( $this->dodavatel == 0 ) {
			$this->_error = JText::_( 'fa_EDIT_CHYBA_PARTNER' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}
          
    //*** kontrola vyplnenie predmetu faktury
    else if ( $this->title == '' ) {
			$this->_error = JText::_( 'fa_EDIT_CHYBA_PREDMET' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}
    
    //*** kontrola vyplnenie sumy faktury
    if ($this->suma == '' || $this->mena == '') {
			$this->_error = JText::_( 'fa_EDIT_CHYBA_SUMA' );
          JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
          return false;
      } 
    
    //*** kontrola identifikacie zmluvy a externej zmluvy, aby neboli byplnene obe !
    if ((!$this->nastavenie->crz) && (!empty($this->vlastni1) || !empty($this->jedcislo)) && $this->typ) {    
			$this->_error = JText::_( 'fa_EDIT_CHYBA_IDENT_ZMLU' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
    } 
      

    //*** kontrola formatu datumu zverejnenia pred ulozenim do DB v formate Y-m-d 
    if (!preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", jzmluvyHelper::datymd($this->publish_up))) {
	      	$this->_error = JText::_( 'fa_EDIT_CHYBA_DAT_DORUCENIA_FORMAT');
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
		}
        
    //*** povinne pole pre datum dorucenia
    if ($this->publish_up == 0) {
        	$this->_error = JText::_( 'fa_EDIT_CHYBA_DAT_DORUCENIA' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
          }
          
    if ($this->datZver == 0) {
        	$this->_error = JText::_( 'fa_EDIT_CHYBA_DAT_ZVEREJNENIE' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
          }
                       
    /*
    if ($this->datUhrady == 0) {
        	$this->_error = JText::_( 'fa_EDIT_CHYBA_DAT_UHRADY' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
          }
      */
    //*** preformatovanie ukladaneho datumu do DB z d.m.Y na Y-m-d
    if(!empty($this->datUhrady) || $this->datUhrady != 0) { 
      $this->datUhrady = jzmluvyHelper::datymd($this->datUhrady);;
      }
    
    //*** preformatovanie ukladaneho datumu do DB z d.m.Y na Y-m-d
    if(!empty($this->datZver) || $this->datZver != 0) { 
       $this->datZver = jzmluvyHelper::datymd($this->datZver);
      }
    //*** preformatovanie ukladaneho datumu do DB z d.m.Y na Y-m-d
    if(!empty($this->publish_down) || $this->publish_down != 0) { 
      $this->publish_down = jzmluvyHelper::datymd($this->publish_down);
      }
    //*** preformatovanie ukladaneho datumu do DB z d.m.Y na Y-m-d
    if(!empty($this->publish_up) || $this->publish_up != 0) { 
      $this->publish_up = jzmluvyHelper::datymd($this->publish_up);
      }
    
    
    if (!preg_match( '/^[\-+]?[0-9]*\.*\,?[0-9]+$/', $this->suma)) {
    $this->_error = JText::_( 'fa_EDIT_CHYBA_SUMA_FORMAT' );
	    	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	     	return false;
         }
         else
         {
    $this->suma = jzmluvyHelper::format_number($this->suma, '.', '');     
         }

    //*** kontrola povolenej pripony pre obrazkove/scanovane subory */
		if ( $this->file['name'] != '' ) {
      $priponaSouboru = strtolower(JFile::getExt($this->file));
			$povoleneExt = explode(',',$this->params->get( 'povolene_pripony_scan', ''));		
        if (!in_array($priponaSouboru, $povoleneExt)) {
          $this->_error = JText::_( 'fa_EDIT_CHYBA_TYP').':&nbsp;'.implode($povoleneExt,', ');
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
             }
      }
          
      //*** kontrola povolenej pripony pre textove subory */
      if ( $this->file2['name'] != '' ) {
      $priponaSouboru = strtolower(JFile::getExt($this->file2));
			$povoleneExt = explode(',',$this->params->get( 'povolene_pripony_text', ''));		
        if (!in_array($priponaSouboru, $povoleneExt)) {
          $this->_error = JText::_( 'fa_EDIT_CHYBA_TYP').':&nbsp;'.implode($povoleneExt,', ');
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
        }
      }
  
 

		return true;
	}
}

?>